﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using BackendDeveloper_WS.Services;
using BackendDeveloper_WS.Models;

namespace BackendDeveloper_WS.Controllers
{
    public class WorkOrdersController : ApiController
    {
        // GET api/WorkOrders
        public IEnumerable<WorkOrder> GetOrders()
        {
            OrderServices os = new OrderServices();
            return os.getWorkOrdersList();
        }

        // GET api/WorkOrders
        public IEnumerable<WorkOrder> GetOrdersByPos(double lat, double lon)
        {
            OrderServices os = new OrderServices();
            return os.getWorkOrdersList(lat, lon);
        }

        // GET api/WorkOrders/5
        public WorkOrder GetOrdersById(int id)
        {
            OrderServices os = new OrderServices();
            WorkOrder wo= os.getWorkOrder(id);
            if (wo == null)
            {
                throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.NotFound));
            }
            else
            {
                return wo;
            }
            
        }

        // POST api/WorkOrders/workOrder/
        public HttpResponseMessage PostOrder(WorkOrder workOrder) //[FromBody]
        {
            OrderServices os = new OrderServices();
            if (!os.createNewWorkOrder(workOrder.description, workOrder.address, workOrder.latitude, workOrder.longitude))
            {
                return new HttpResponseMessage(HttpStatusCode.InternalServerError);

            }
            else {
                return new HttpResponseMessage(HttpStatusCode.OK);
            }
        }

        // PUT api/WorkOrders/5
        public HttpResponseMessage PutOrder(int id, WorkOrder workOrder)
        {
            OrderServices os = new OrderServices();
            if(!os.editWorkOrder(id, workOrder.description, workOrder.address, workOrder.latitude, workOrder.longitude)){
                return new HttpResponseMessage(HttpStatusCode.NotFound);
            }
            else
            {
                return new HttpResponseMessage(HttpStatusCode.OK);
            }
        }

    }
}